package com.molichuxing.gateway.bff.orders.services.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.gateway.bff.orders.convertor.Convert;
import com.molichuxing.gateway.bff.orders.entity.request.create.OrderPayeeCreateVo;
import com.molichuxing.gateway.bff.orders.entity.request.modify.RenegeCancelModifyVo;
import com.molichuxing.gateway.bff.orders.entity.response.OrderPayeeVo;
import com.molichuxing.gateway.bff.orders.services.OrderRenegeBffService;
import com.molichuxing.services.business.dto.response.OrderRenegeBizDto;
import com.molichuxing.services.business.service.OrderRenegeBizService;
import com.molichuxing.services.infrastructure.dto.response.OrderExclusivelDto;
import com.molichuxing.services.infrastructure.dto.response.UserDto;
import com.molichuxing.services.infrastructure.dto.response.UserPayeeDto;
import com.molichuxing.services.infrastructure.service.OrderExclusivelService;
import com.molichuxing.services.infrastructure.service.OrderRenegeService;
import com.molichuxing.services.infrastructure.service.UserPayeeService;
import com.molichuxing.services.infrastructure.service.UserService;
import com.molichuxing.services.property.OrderExclusivelStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 订单退租相关bff
 * 
 * @author zoumingyu
 * @date 2020年2月27日
 *
 */
@Service("orderRenegeBffService")
public class OrderRenegeBffServiceImpl implements OrderRenegeBffService {
	private static final Logger logger = LoggerFactory.getLogger(OrderRenegeBffServiceImpl.class);

	@Resource
	private OrderRenegeBizService orderRenegeBizService;

	@Resource
	private OrderRenegeService orderRenegeService;

	@Resource
	private UserPayeeService userPayeeService;

	@Resource
	private UserService userService;

	@Resource
	private OrderExclusivelService orderExclusivelService;

	/**
	 * 取消退租
	 *
	 * @param modifyVo
	 * @return
	 * @throws Exception
	 * @throws BizException
	 */
	@Override
	public Boolean modifyCancel(RenegeCancelModifyVo modifyVo) throws BizException, Exception {
		return orderRenegeBizService.modifyCancel(Convert.toOrderRenegeCancelModifyBizDto(modifyVo));
	}

	/**
	 * 查询订单退租人信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @param userId
	 *            用户id
	 * @return
	 */
	@Override
	public OrderPayeeVo getOrderPayee(Long orderCode, Integer userId) throws BizException, Exception {
		OrderPayeeVo result = null;
		// 查询订单退租人信息
		OrderRenegeBizDto orderRenegeBizDto = orderRenegeBizService.getOrderRenege(orderCode, userId);

		if (StringUtils.isBlank(orderRenegeBizDto.getPayee())) {
			// 还未填写退租人信息，查询用户信息
			UserDto user = userService.getUser(userId);
			result = new OrderPayeeVo();
			result.setPayee(user.getRealName());
			result.setOrderCode(orderCode);
			result.setIsWrite(false);
			return result;
		}

		result = Convert.toOrderPayeeVo(orderRenegeBizDto);
		// 已经填写退租人信息，查询订单信息
		OrderExclusivelDto orderExclusivel = orderExclusivelService.getOrderExclusivel(orderCode);
		if (OrderExclusivelStatusEnum.RENEGE_ACCEPTED.equals(orderExclusivel.getOrderStatus())
				|| OrderExclusivelStatusEnum.RENEGE_SETTLEMENT.equals(orderExclusivel.getOrderStatus())
				|| OrderExclusivelStatusEnum.RENEGE_REFUND.equals(orderExclusivel.getOrderStatus())) {
			// 待还车、财务结算、解除协议确认等状态存在退租人信息，还需要提交填写
			result.setIsWrite(false);
		} else {
			// 其他状态存在退租人信息，不可修改
			result.setIsWrite(true);
		}

		// 不存在退租信息，查询用户信息
//		UserPayeeDto userPayeeDto = userPayeeService.getUserPayee(userId);
//		if (userPayeeDto == null) {
//			result = new OrderPayeeVo();
//		} else {
//			result = Convert.toOrderPayeeVo(userPayeeDto);
//		}

		return result;
	}

	/**
	 * 订单退租退款人信息新增
	 * 
	 * @param createVo
	 * @return
	 */
	@Override
	public Boolean createPayee(OrderPayeeCreateVo createVo) throws BizException, Exception {
		Boolean result = false;
		// 查询用户退租人信息
		UserPayeeDto userPayeeDto = userPayeeService.getUserPayee(createVo.getUserId());
		if (userPayeeDto == null) {
			// 不存在，新增
			result = userPayeeService.create(Convert.toUserPayeeCreateDto(createVo));
		} else {
			// 存在，修改
			result = userPayeeService.modify(Convert.toUserPayeeModifyDto(createVo));
		}
		if (result) {
			// 修改订单信息并修改退租信息
			orderRenegeBizService.modifyPayee(Convert.toOrderRenegePayeeModifyBizDto(createVo));
		}

		return result;
	}
}
